﻿using B.R.RabbitRoot.Domain;
using B.R.RabbitRoot.ErrorCode;
using B.R.RabbitRoot.Infrastructure.Interface;
using B.R.RabbitRoot.Read.API.Applications.Commands.SourceInfoCommands;
using B.R.RabbitRoot.Read.API.Dtos;
using MediatR;

namespace B.R.RabbitRoot.Read.API.Applications.CommandHandlers.SourceInfoCommandHandlers
{
    /// <summary>
    /// 溯源显示命令处理
    /// </summary>
    public class SourceInfoQueryCommandHandler : IRequestHandler<SourceInfoQueryCommand, APIResult<APIPaging<SourceInfoDTO>>>
    {
        private readonly IBaserepository<SourceInfo> sourceInfoRepsitoty;
        private readonly IBaserepository<Account> accountRepsitoty;
        private readonly IBaserepository<product> productRepsitoty;

        public SourceInfoQueryCommandHandler(IBaserepository<SourceInfo> sourceInfoRepsitoty, IBaserepository<Account> accountRepsitoty, IBaserepository<product> productRepsitoty)
        {
            this.sourceInfoRepsitoty = sourceInfoRepsitoty;
            this.accountRepsitoty = accountRepsitoty;
            this.productRepsitoty = productRepsitoty;
        }


        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<APIPaging<SourceInfoDTO>>> Handle(SourceInfoQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<SourceInfoDTO>> result=new APIResult<APIPaging<SourceInfoDTO>>();
            result.Code=ResultCode.显示成功;
            result.Message=ResultCode.显示成功.ToString();
            var entity = from a in sourceInfoRepsitoty.GetAll()
                         join b in accountRepsitoty.GetAll() on a.AccountId equals b.AccountId
                         join c in productRepsitoty.GetAll() on a.ProductNameId equals c.ProductNameId
                         where !a.DeleteIs
                         select new SourceInfoDTO
                         {
                               SourceInfoId=a.SourceInfoId,
                             AccountId=a.AccountId,
                             AccountName=b.AccountName,
                             AccountNickName=b.AccountNickName,
                             ProductNameId=a.ProductNameId,
                             ProductName=c.ProductName,
                             Sourceproduction=a.Sourceproduction,
                             SourceexpirationTime=a.SourceexpirationTime,
                             Sourcelicence= a.Sourcelicence,
                             SourceCreamFirm= a.SourceCreamFirm,
                             SourCecoverPicture=a.SourCecoverPicture,
                             Completeness=a.Completeness,
                             SourceCreamPerson=a.SourceCreamPerson,
                             SourceCreamTime=a.SourceCreamTime,
                             SourceUpdatePerson=a.SourceUpdatePerson,
                             DeleteIs=a.DeleteIs,
                         };
            if (request.SourceInfoId !=0)
            {
                entity = entity.Where(m => m.SourceInfoId == request.SourceInfoId);
            }
            //var entity=sourceInfoRepsitoty.GetAll();
            APIPaging <SourceInfoDTO> page = new APIPaging<SourceInfoDTO>();
            page.RecordCount = entity.Count();
            page.PageCount = (int)Math.Ceiling(page.RecordCount*1.0/request.PageSize);
            page.Data = entity.OrderByDescending(m => m.SourceInfoId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Data = page;
            return await Task.FromResult(result);
        }
    }
}
